package com.cssl.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.ByteArrayInputStream;

public class FileUpload {

    // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
    private  String ENDPOINT;
    private  String ACCESS_KEY_ID;
    private  String ACCESS_KEY_SECRET;
    private  String BUCKETNAME;


    public String getENDPOINT() {
        return ENDPOINT;
    }

    public void setENDPOINT(String ENDPOINT) {
        this.ENDPOINT = ENDPOINT;
    }

    public String getACCESS_KEY_ID() {
        return ACCESS_KEY_ID;
    }

    public void setACCESS_KEY_ID(String ACCESS_KEY_ID) {
        this.ACCESS_KEY_ID = ACCESS_KEY_ID;
    }

    public String getACCESS_KEY_SECRET() {
        return ACCESS_KEY_SECRET;
    }

    public void setACCESS_KEY_SECRET(String ACCESS_KEY_SECRET) {
        this.ACCESS_KEY_SECRET = ACCESS_KEY_SECRET;
    }

    public String getBUCKETNAME() {
        return BUCKETNAME;
    }

    public void setBUCKETNAME(String BUCKETNAME) {
        this.BUCKETNAME = BUCKETNAME;
    }

    public FileUpload() {
    }

    public FileUpload(String ENDPOINT, String ACCESS_KEY_ID, String ACCESS_KEY_SECRET, String BUCKETNAME) {
        this.ENDPOINT = ENDPOINT;
        this.ACCESS_KEY_ID = ACCESS_KEY_ID;
        this.ACCESS_KEY_SECRET = ACCESS_KEY_SECRET;
        this.BUCKETNAME = BUCKETNAME;
    }

    public String upload(byte[] bytes, String objectName) {
        System.out.println("ENDPOINT = " + ENDPOINT);
        System.out.println("ENDPOINT = " + ACCESS_KEY_ID);
        System.out.println("ENDPOINT = " + ACCESS_KEY_SECRET);
        System.out.println("ENDPOINT = " + BUCKETNAME);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        try {
            // 创建PutObject请求。
            ossClient.putObject(BUCKETNAME, objectName, new ByteArrayInputStream(bytes));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        stringBuilder
                .append(BUCKETNAME)
                .append(".")
                .append(ENDPOINT)
                .append("/")
                .append(objectName);

        System.out.println("文件上传到:{}"+stringBuilder.toString());

        return stringBuilder.toString();
    }
}
